package com.yyh.fanshe;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Properties;

public class Demo9 {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        //需求：使用类加载器读取配置文件pro. properties中的配置信息并存入Properties集合中
        //1.获得当前类文件的类加载器并读取pro. properties文件中内容到流中
        ClassLoader classLoader = Demo9.class.getClassLoader();
        InputStream resourceAsStream = classLoader.getResourceAsStream("pro.properties");
        //2.将resourceAsStream流中的内容读取到Properties集合中
        Properties properties = new Properties();
        properties.load(resourceAsStream);
        //3.根据全类名获取类文件对象
        String className = (String)properties.get("className");
        Class aClass = Class.forName(className);
        //4.根据类文件对象创建实例化对象
        User user = (User) aClass.newInstance();
        user.setName("张三");
        //5.获取要执行的方法名，根据要执行的方法名获取方法对象并调用
        String methodName = (String)properties.get("methodName");
        Method method = aClass.getMethod(methodName);
        //6.执行该user对象中的method方法
        System.out.println(method.invoke(user));
    }
}
